﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace BlueMonkey.View
{
    /// <summary>
    /// Interaction logic for VideoPlayer.xaml
    /// </summary>
    public partial class VideoPlayer : UserControl
    {
        public VideoPlayer()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VideoPlayer_Loaded);
        }

        void VideoPlayer_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_media.Path != string.Empty)
                {
                    vdoElement.LoadedBehavior = MediaState.Play;
                    vdoElement.Play();
                }
            }
            catch { }
            //throw new NotImplementedException();
        }

        public event EventHandler OnStop = delegate { };
        public event EventHandler OnShowMenu = delegate { };
        public event EventHandler OnHideMenu = delegate { };

        private TimeSpan TotalTime;
        DispatcherTimer timerVideoTime;
        private int _osd = 0;
        private bool isCapture = false;
        private BlueCore.MediaObject _media;

        public BlueCore.MediaObject Media
        {
            get { return _media; }
            set { _media = value; }
        }

        public void Stop()
        {
            vdoElement.Stop();
            GC.SuppressFinalize(this);
        }

        bool IsPlaying()
        {
            var pos1 = vdoElement.Position;
            System.Threading.Thread.Sleep(30);
            var pos2 = vdoElement.Position;

            return (pos2 - pos1).TotalMilliseconds > 20;
        }

        private void buttonPause_Click(object sender, RoutedEventArgs e)
        {
            if (IsPlaying())
            {
                vdoElement.Pause();
                buttonPause.Content = "Play";
            }
            else
            {
                if (vdoElement.Source == null)
                {
                    vdoElement.Source = new Uri(_media.Path);
                }
                vdoElement.Play();
                buttonPause.Content = "Pause";
            }
        }
        #region button
        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {

            TimeSpan ts = TimeSpan.FromSeconds(0);
            vdoElement.Position = ts;

            buttonPause.Content = "Play";
            vdoElement.Stop();
            OnStop(this, e);
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan curTime = vdoElement.Position;
            TimeSpan ts = TimeSpan.FromSeconds(10);
            vdoElement.Position = curTime.Subtract(ts);
        }

        private void buttonForward_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan curTime = vdoElement.Position;
            TimeSpan ts = TimeSpan.FromSeconds(10);
            vdoElement.Position = curTime.Add(ts);
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            vdoElement.SpeedRatio = 1.0;
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            vdoElement.SpeedRatio = 1.5;
        }

        private void RadioButton_Checked_2(object sender, RoutedEventArgs e)
        {
            vdoElement.SpeedRatio = 2.0;
        }

        private void vdoElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            TotalTime = vdoElement.NaturalDuration.TimeSpan;

            slider1.Maximum = TotalTime.TotalSeconds;
            // Create a timer that will update the counters and the time slider
            timerVideoTime = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
            timerVideoTime.Tick += new EventHandler(timer_Tick);
            timerVideoTime.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            _osd += 1;
            #region Check if the movie loaded calculate it's total time
            if (vdoElement.NaturalDuration.TimeSpan.TotalSeconds > 0)
            {
                if (TotalTime.TotalSeconds > 0)
                {
                    // Updating time slider
                    slider1.Value = vdoElement.Position.TotalSeconds;
                }
            }
            #endregion
            if (_osd > 5)
            {
                controlBar.Visibility = System.Windows.Visibility.Collapsed;
                OnHideMenu(this, null);
            }
        }

        private void vdoElement_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //trig open
            _osd = -1;
            controlBar.Visibility = System.Windows.Visibility.Visible;
            OnShowMenu(this, null);
        }

        private void slider1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (TotalTime.TotalSeconds > 0)
            {
                vdoElement.Position = TimeSpan.FromSeconds(slider1.Value);
            }
        }
        #endregion

        private void vdoElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            buttonPause.Content = "Play";
            vdoElement.Position = new TimeSpan(0, 0, 0);
            vdoElement.Stop();
            OnStop(this, e);
        }

        Point ptCapture;
        Type typeCapture = null;
        private void vdoElement_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isCapture = true;
            ptCapture = e.GetPosition(this);
            typeCapture = sender.GetType();
            posInfo.Visibility = System.Windows.Visibility.Visible;
        }

        #region mouse event
        Point ptRelease;
        Type typeRelese = null;
        Point ptLastpost;
        private void vdoElement_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isCapture = false;
            ptRelease = e.GetPosition(this);
            typeRelese = sender.GetType();
            if (typeCapture != null)
            {
                double diff = ptRelease.X - ptCapture.X;
                double pos = diff / vdoElement.ActualWidth;
                double totalSec = pos * 20;

                TimeSpan curTime = vdoElement.Position;
                TimeSpan ts = TimeSpan.FromSeconds(totalSec);
                vdoElement.Position = curTime.Add(ts);
                Console.WriteLine(totalSec);
            }
            posInfo.Visibility = System.Windows.Visibility.Collapsed;
            typeCapture = null;
            typeRelese = null;

            //Console.WriteLine(sender.GetType() + "mouse up" + diff + "/" + vdoElement.ActualWidth);
        }

        private void vdoElement_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isCapture)
            {
                if (typeCapture != null)
                {
                    Point pt = e.GetPosition(this);
                    ptLastpost = pt;
                    double diff = pt.X - ptCapture.X;
                    //Console.WriteLine(pt.X + "-" + ptCapture.X+"="+diff);
                    double pos = diff / vdoElement.ActualWidth;
                    double totalSec = pos * 20;
                    if (totalSec > 0)
                        posInfo.Text = String.Format("{0:+0.00}", totalSec);
                    else
                        posInfo.Text = String.Format("{0:0.00}", totalSec);
                    //posInfo.v
                    TimeSpan curTime = vdoElement.Position;
                    TimeSpan ts = TimeSpan.FromSeconds(totalSec);
                    //vdoElement.Position = curTime.Add(ts);
                    //Console.WriteLine(totalSec);
                }
            }
        }

        private void posInfo_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isCapture = false;
            ptRelease = e.GetPosition(this);
            typeRelese = sender.GetType();
            if (typeCapture != null)
            {
                double diff = ptRelease.X - ptCapture.X;
                double pos = diff / vdoElement.ActualWidth;
                double totalSec = pos * 20;

                TimeSpan curTime = vdoElement.Position;
                TimeSpan ts = TimeSpan.FromSeconds(totalSec);
                vdoElement.Position = curTime.Add(ts);
                Console.WriteLine(totalSec);
            }
            posInfo.Visibility = System.Windows.Visibility.Collapsed;
            typeCapture = null;
            typeRelese = null;
        }

        private void posInfo_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isCapture)
            {
                if (typeCapture != null)
                {
                    Point pt = e.GetPosition(posInfo);
                    double diff = (pt.X + ptLastpost.X) - ptCapture.X;
                    double pos = diff / vdoElement.ActualWidth;
                    double totalSec = pos * 20;
                    if (totalSec > 0)
                        posInfo.Text = String.Format("{0:+0.00}", totalSec);
                    else
                        posInfo.Text = String.Format("{0:0.00}", totalSec);
                    //posInfo.v
                    TimeSpan curTime = vdoElement.Position;
                    TimeSpan ts = TimeSpan.FromSeconds(totalSec);
                    //Console.WriteLine(totalSec);
                }
            }
        }
        #endregion
    }
}
